package com.example.kuaizu.aop;

import com.example.kuaizu.utils.AjaxResponse;
import kuaizu.common.exception.MassageException;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.dromara.x.file.storage.core.exception.FileStorageRuntimeException;
import org.springframework.stereotype.Component;

/**
 * @author: wangmx15
 * @date: 2025/8/31
 **/
@Aspect
@Component
@Log4j2
@RequiredArgsConstructor
public class LogAspect {
    //给每个controller方法添加日志切面
    @Around("execution(* com.example.kuaizu.controller.*.*(..))")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("开始执行方法：{}->方法参数{}", joinPoint.getSignature().getName(), joinPoint.getArgs());
        Object result = null;
        try {
            result = joinPoint.proceed();
        }catch (MassageException | FileStorageRuntimeException e){
            log.error("方法执行异常：{}->异常信息{}", joinPoint.getSignature().getName(), e.getMessage());
            return (Object) AjaxResponse.error(e.getMessage());
        }
        log.info("方法执行完毕：{}->返回参数{}", joinPoint.getSignature().getName(), result);
        return result;
    }
}
